#pragma  once

#include "stdafx.h"

#include "items.h"
using namespace std;

class ItemScene;
class SceneView;
class Connector;
class ConnectionPoint;
class Spot;
class Title;
class Indicator;

class Line : public QObject, public QGraphicsLineItem
{
	Q_OBJECT
public:

	Line(const QPointF &_source, const QPointF &_dest);
	~Line(){}
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	QPointF source, dest;
	enum pipe_orientation_t{VERTICAL, HORIZONTAL, ZERO, NONE};
	pipe_orientation_t orientation();
	void setMovable(bool _allow_move){ allow_move = _allow_move; }
	double getLength();
	QPointF getPointfromPercentPos(const double &_percent_pos);
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
	bool allow_move;
	bool movable;
signals:
	void positionChanged(QWeakPointer<Line>);
	void selectionChanged(bool selected);
};
//===============================================================================================================
class Connector: public QGraphicsObject
{
	Q_OBJECT
public:
	Connector(QWeakPointer<SceneView> _scene, QWeakPointer<ConnectionPoint> _source, QWeakPointer<ConnectionPoint> _dest, QSharedPointer<Spot> _spot, const QString &_coordinates);
	~Connector();
	void adjust();
	QRectF boundingRect() const{return QRectF();};
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
	bool isSelected();
	QWeakPointer<ConnectionPoint> source, dest;
	QString get_lines_coordinate(int from_index = -1, QPointF from_pos = QPointF());
	pair<QPointF, int> intersected(const QPointF &_point);
	vector<QSharedPointer<Line>> lines;
	void setMovable(bool movable){ foreach(QSharedPointer<Line> line, lines) line->setMovable(movable);}
	QPointF getMiddlePoint();
	QSharedPointer<Spot> spot;
	void addLine(int index, const QPointF &_source, const QPointF &_dest);
private:
	void add_lines(const QString &coordinates);
	void resizeNext(int index, const QPointF &new_point);
	void resizePrevious(int index, const QPointF &new_point);
	
	void deleteLine(int index);
	QString getDefaultName();
	QWeakPointer<SceneView>  scene;
	int findIndex(QWeakPointer<Line> line);
	
private slots:
	void userChangePosition(QWeakPointer<Line>);
};
//===============================================================================================================
class virtualConnector: public QGraphicsObject
{
	Q_OBJECT
public:
	virtualConnector(QWeakPointer<SceneView> _scene, QWeakPointer<ConnectionPoint> _source, const QPointF &_dest_point);
	~virtualConnector();
	void adjust(const QPointF &_dest_point);
	QRectF boundingRect() const{return QRectF();};
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
	QWeakPointer<ConnectionPoint> source;
private:
	void addLines(int count);
	vector<QSharedPointer<Line>> lines;
	QWeakPointer<SceneView>  scene;
};

//=====================================================================================================================
class Spot : public QGraphicsObject
{
	Q_OBJECT

public:
	Spot(QWeakPointer<SceneView> _scene);
	~Spot();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	void adjust();
	void addSource(QWeakPointer<Connector> _source){ source = _source; }
	QPointF findClosestPoint(const QPointF &_point);
	
	void setTitle(QSharedPointer<Title> _title){ title = _title; }
	QString getTitleText(){ return title.data()->getText(); }
	QPointF getTitlePos(){ return title.data()->pos(); } 
	void setTitlePos(const QPointF &_pos){ title.data()->setPos(_pos); } 
	void setTitleText(const QString &_text){ title->setText(_text); }
	
	void setSensor(Sensor* _sensor){ sensor = _sensor; }
	Sensor* getSensor(){ return sensor; }
	//void setIndicator(QSharedPointer<Indicator> _indicator){ indicator = _indicator; }
	//QSharedPointer<Indicator> indicator;
private:
	Sensor *sensor;
	QWeakPointer<SceneView> scene;
	QWeakPointer<Connector> source;
	QSharedPointer<Title> title;
	int line_index;
	double percent_pos;
	Line::pipe_orientation_t orientation;
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private slots:
	void showIndicator();
	void showSensorEditor();
	void showIndicatorLine();
	void removeItem();
};

